﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto;
using SqlSugar;

namespace Zocono.WCS.Domain.BytesMapObjectDomain
{
    public class BytesMapObjectInfoService : IBytesMapObjectInfoService
    {
        private readonly ILogUtil _logUtil;
        private readonly IBytesMapObjectInfoRepository _BytesMapObjectInfoRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public BytesMapObjectInfoService(ISnowflake snowflake, IBytesMapObjectInfoRepository BytesMapObjectInfoRepository, ILogUtil logUtil, IMapper mapper)
        {
            _BytesMapObjectInfoRepository = BytesMapObjectInfoRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddBytesMapObjectInfoAsync(BytesMapObjectInfoDto BytesMapObjectInfoDto)
        {
            var BytesMapObjectInfo = _Mapper.Map<BytesMapObjectInfoDto, BytesMapObjectInfo>(BytesMapObjectInfoDto);
            long id = _Snowflake.GetId();
            BytesMapObjectInfo.ID = id;
            bool result = await _BytesMapObjectInfoRepository.InsertAsync(BytesMapObjectInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeleteBytesMapObjectInfoAsync(long ID)
        {
            bool result = await _BytesMapObjectInfoRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdateBytesMapObjectInfoAsync(BytesMapObjectInfoDto BytesMapObjectInfoDto)
        {
            var BytesMapObjectInfo = _Mapper.Map<BytesMapObjectInfoDto, BytesMapObjectInfo>(BytesMapObjectInfoDto);
            bool result = await _BytesMapObjectInfoRepository.UpdateAsync(BytesMapObjectInfo);
            return result;
        }

        public async Task<PageDto<BytesMapObjectInfoDto>> GetBytesMapObjectInfoAsync(QueryBytesMapObjectInfoDto queryBytesMapObjectInfoDto)
        {
            PageModel page = new PageModel { PageIndex = queryBytesMapObjectInfoDto.PageIndex, PageSize = queryBytesMapObjectInfoDto.PageSize, };
            var objectName = queryBytesMapObjectInfoDto.ObjectName;
            var plcReadDbId = queryBytesMapObjectInfoDto.PlcReadDbId;
            var exp = Expressionable.Create<BytesMapObjectInfo, PlcReadDbInfo>();
            exp.AndIF(!string.IsNullOrEmpty(objectName), (a, b) => a.ObjectName == objectName);
            exp.AndIF(!string.IsNullOrEmpty(queryBytesMapObjectInfoDto.EquipmentCode), (a, b) => b.EquipmentCode.Contains(queryBytesMapObjectInfoDto.EquipmentCode));
            exp.AndIF(plcReadDbId.HasValue && plcReadDbId != 0, (a, b) => a.PlcReadDbId == plcReadDbId);
            exp.AndIF(plcReadDbId.HasValue && plcReadDbId != 0, (a, b) => a.PlcReadDbId == plcReadDbId);

            return await _BytesMapObjectInfoRepository.GetBytesMapObjectInfoAsync(exp.ToExpression(), page);
        }

        public async Task<BytesMapObjectInfoDto> GetBytesMapObjectInfoByIDAsync(long ID)
        {
            var result = await _BytesMapObjectInfoRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<BytesMapObjectInfo, BytesMapObjectInfoDto>(result);
            return vModel;
        }
    }
}
